package com.flydog.mall.controller.pms;

import com.flydog.mall.common.api.CommonResult;
import com.flydog.mall.common.api.PageResponse;
import com.flydog.mall.dto.PmsBrandParam;
import com.flydog.mall.entity.pms.PmsBrand;
import com.flydog.mall.service.pms.PmsBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author AI
 */

@Slf4j
@Controller
@RequestMapping("/brand")
public class PmsBrandController {

    @Autowired
    private PmsBrandService brandService;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> create(@RequestBody PmsBrandParam brandParam) {
        int code = brandService.create(brandParam);
        return CommonResult.success(code);
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PmsBrand> getById(@PathVariable("id") long brandId) {
        Optional<PmsBrand> result = brandService.getBrand(brandId);
        return result.map(CommonResult::success).orElseGet(CommonResult::failed);
    }

    @RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Integer> delete(@PathVariable("id") long id) {
        try{
            Integer count = brandService.delete(id);
            return CommonResult.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("文件上传错误：{}", e.getMessage());
        }
        return CommonResult.failed();
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageResponse<PmsBrand>> list(@RequestParam(value = "keyword", required = false) String keyword,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize) {
        return CommonResult.success(brandService.listBrand(keyword, pageNum, pageSize));
    }

    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<PmsBrand>> listAll() {
        return CommonResult.success(brandService.listAll());
    }

    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> update(@PathVariable("id") long id, @RequestBody PmsBrandParam pmsBrand) {
        int count = brandService.update(id, pmsBrand);
        return CommonResult.success(count);
    }

    @RequestMapping(value = "/update/showStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> updateShowStatus(@RequestParam("ids") List<Long> ids,
                                                  @RequestParam("showStatus") Integer showStatus) {
        int count = brandService.updateShowStatus(ids, showStatus);
        return CommonResult.success(count);
    }


    @RequestMapping(value = "/update/factoryStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<Integer> updateFactoryStatus(@RequestParam("ids") List<Long> ids,
                                                  @RequestParam("factoryStatus") Integer factoryStatus) {
        int count = brandService.updateFactoryStatus(ids, factoryStatus);
        return CommonResult.success(count);
    }
}
